package xyz.lisys.groovy

import org.codehaus.groovy.runtime.DefaultGroovyMethods

// 定义二级闭包格式
class Conf {
    String name
    int age

    Conf name(String name) {
        this.name = name
        return this
    }

    Conf age(int age) {
        this.age = age
        return this
    }
}

// 定义一级闭包格式，即脚本的格式
void user(@DelegatesTo(Conf.class) Closure<Conf> closure) {
    Conf conf = new Conf()
    DefaultGroovyMethods.with(conf, closure)
    println "my name is ${conf.name} my age is ${conf.age}"
}

user {
    name 'zyp'
    age 12
}

def user2(@DelegatesTo.Target Conf conf, @DelegatesTo Closure<Conf> closure) {
    // rehydrate方法创建一个闭包副本
    closure.rehydrate(conf, conf, conf)()
    println "my name is ${conf.name} my age is ${conf.age}"
}

user2(new Conf(), {
    name 'zyp'
    age 12
})
